﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package reprise.commands {
    import flash.events.*;
    import reprise.core.*;
    import reprise.data.collection.*;

    public class FrameCommandExecutor {

        protected var m_nextKeyIndex:int;
        private var m_commands:HashMap;

        protected static var g_instance:FrameCommandExecutor;

        public function FrameCommandExecutor(){
            m_commands = new HashMap();
            m_nextKeyIndex = 0;
        }
        protected function wrapperForName(_arg1:String):Object{
            return (m_commands.objectForKey(_arg1));
        }
        public function removeCommand(_arg1:ICommand):void{
            m_commands.removeObject(wrapperForCommand(_arg1));
            if (!m_commands.size()){
                ApplicationRegistry.instance().applicationForURL("").stage.removeEventListener(Event.ENTER_FRAME, enterFrame);
            };
        }
        public function delayCommand(_arg1:ICommand):void{
            if (containsCommand(_arg1)){
                return;
            };
            if (!m_commands.size()){
                ApplicationRegistry.instance().applicationForURL("").stage.addEventListener(Event.ENTER_FRAME, enterFrame);
            };
            var _local2:Object = {command:_arg1, oneOff:true};
            m_commands.setObjectForKey(_local2, getNextKey());
        }
        public function addCommand(_arg1:ICommand):void{
            addCommandWithName(_arg1, getNextKey());
        }
        protected function containsCommand(_arg1:ICommand):Boolean{
            return (!((wrapperForCommand(_arg1) == null)));
        }
        public function enterFrame(_arg1:Event):void{
            var _local3:Object;
            var _local4:String;
            var _local2:Object = m_commands.toObject();
            for (_local4 in _local2) {
                _local3 = _local2[_local4];
                _local3.command.execute();
                if (_local3.oneOff){
                    removeCommandWithName(_local4);
                };
            };
        }
        protected function getNextKey():String{
            return (("key" + m_nextKeyIndex++));
        }
        public function addCommandWithName(_arg1:ICommand, _arg2:String):void{
            if (containsCommand(_arg1)){
                return;
            };
            if (!m_commands.size()){
                ApplicationRegistry.instance().applicationForURL("").stage.addEventListener(Event.ENTER_FRAME, enterFrame);
            };
            var _local3:Object = {command:_arg1};
            m_commands.setObjectForKey(_local3, _arg2);
        }
        protected function wrapperForCommand(_arg1:ICommand):Object{
            var _local3:Object;
            var _local4:String;
            var _local2:Object = m_commands.toObject();
            for (_local4 in _local2) {
                _local3 = _local2[_local4];
                if (_local3.command == _arg1){
                    return (_local3);
                };
            };
            return (null);
        }
        public function removeCommandWithName(_arg1:String):void{
            m_commands.removeObject(wrapperForName(_arg1));
            if (!m_commands.size()){
                ApplicationRegistry.instance().applicationForURL("").stage.removeEventListener(Event.ENTER_FRAME, enterFrame);
            };
        }

        public static function instance():FrameCommandExecutor{
            if (!g_instance){
                g_instance = new (FrameCommandExecutor);
            };
            return (g_instance);
        }

    }
}//package reprise.commands 
